// pages/history/history.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defultImg: "https://pic2.zhimg.com/v2-ab9549c7127bef7b692021e83d851f07_1440w.jpg?source=172ae18b", // 默认图
    historyList: [], // 历史记录
  },

  // 获取历史记录数据
  getHistoryData() {
    if (wx.getStorageSync('historyCatalog')) {
      this.setData({historyList: wx.getStorageSync('historyCatalog')})
    }
    // console.log(this.data.historyList);
  },
  // 前往详情页
  goDetail(e) {
      wx.navigateTo({
        url: '/pages/bookdetail/bookdetail?id='+ e.currentTarget.dataset.bookid +'&title=' + e.currentTarget.dataset.title,
      })
  },
  // 判断是否在书架中
  isInShelf() {
    if (this.data.historyList) {
      let shelf = wx.getStorageSync('bookShelf')
      shelf.forEach((item) => {
      this.data.historyList.forEach((i) => {
        if (i.bookId == item.bookId) {
          i.isInShelf = true
        }
      })
      this.setData({historyList: this.data.historyList})
    })
    }
    console.log(this.data.historyList);
    
  },
  // 加入书架
  addShelf(e) {  
    console.log(e);
    let bookId = e.currentTarget.dataset.bookid
    let list = this.data.historyList
    let index = list.findIndex((item) => item.bookId == bookId)
    console.log(index);
    list[index].isInShelf = true

    if (!wx.getStorageSync('bookShelf')) {
      let obj = {
        bookId: list[index].bookId,
        cover: list[index].cover,
        title: list[index].title,
        author: list[index].author,
      }
      wx.setStorageSync('bookShelf',[obj])
    } else {
      console.log(111);
      let bookshelf = wx.getStorageSync('bookShelf')
      let obj = {
        bookId: list[index].bookId,
        cover: list[index].cover,
        title: list[index].title,
        author: list[index].author,
      }
      wx.setStorageSync('bookShelf',[...new Set([obj,...bookshelf])])
    }
    wx.showToast({
      title: '加入成功 ~',
      icon: 'none',
      success: res => {
        this.setData({
          historyList: list
        })
      }
    })
  
  },
  // 删除阅读记录
  delHistory(e) {
    console.log(e);
    let bookId = e.currentTarget.dataset.bookid
    let list = this.data.historyList
    let index = list.findIndex((item) => item.bookId == bookId)
    console.log(index);
    wx.showModal({
      title: '提示',
      content: '是否删除该阅读记录',
      success: (res) => {
        if (res.confirm) {
          list.splice(index,1)
          this.setData({
            historyList: list
          })
          wx.showToast({
            title: '删除成功 ~',
            icon: 'none',
            success: res => {
              this.setData({
                historyList: list
              })
            }
          })
          wx.setStorageSync('historyCatalog', this.data.historyList)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  onLoad(options) {
    this.getHistoryData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getHistoryData()
    this.isInShelf()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})